package com.zhonghui.procurement.controller;

import com.github.pagehelper.PageHelper;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.procurement.domain.ProcurementList;
import com.zhonghui.procurement.domain.ProcurementMark;
import com.zhonghui.procurement.domain.dto.ProcurementMarkDto;
import com.zhonghui.procurement.domain.vo.ProcurementListDetailsVo;
import com.zhonghui.procurement.domain.vo.PurchaseAnnouncementVo;
import com.zhonghui.procurement.service.ProcurementListService;
import com.zhonghui.procurement.service.ProcurementMarkService;
import com.zhonghui.response.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhonghui.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "采购管理系统-(平台动态,信息公告,政策法规)")
@RestController
@RequestMapping("/procurement")
public class ProcurementListController extends BaseController {

    @Autowired
    private ProcurementListService procurementListService;

    @Autowired
    private ProcurementMarkService procurementMarkService;

    @ApiOperation("(平台动态,信息公告,政策法规)-列表")
    @GetMapping("/getListByPage")
    public BaseResult<List<ProcurementList>> getProcurementList(Integer pageIndex, Integer pageSize, int listType,Integer announcementType,@RequestParam(required = false)String title) {
        System.out.println(announcementType);
        if (pageIndex!=null){
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<ProcurementList> procurementList = procurementListService.getProcurementList(listType,announcementType,title);
        return BaseResult.success(procurementList);
    }

    @ApiOperation("(平台动态,信息公告,政策法规)-详情")
    @GetMapping("/getProcurementListItem")
    public BaseResult<ProcurementListDetailsVo> getProcurementListItem(int id) {
        ProcurementList procurementList = procurementListService.getProcurementListItem(id);
        ProcurementMarkDto tenderEnterpriseStatus = procurementMarkService.getTenderEnterpriseStatus(id);
        ProcurementMark procurementMark = procurementMarkService.selectProcurementMarkByMarkId(Long.valueOf(id));
        ProcurementListDetailsVo procurementListDetailsVo = new ProcurementListDetailsVo();
        procurementListDetailsVo.setProcurementList(procurementList);
        procurementListDetailsVo.setProcurementMarkDto(tenderEnterpriseStatus);
        if(procurementMark != null){
            procurementListDetailsVo.setBigType(procurementMark.getBidType());
        }
        return BaseResult.success(procurementListDetailsVo);
    }

    @ApiOperation("新增采购公告表单")
    @PostMapping("/addPurchaseAnnouncement")
    public BaseResult addPurchaseAnnouncement(@RequestBody PurchaseAnnouncementVo purchaseAnnouncementVo){
        System.out.println(purchaseAnnouncementVo.toString());
        int i = procurementListService.insertProcurementList(purchaseAnnouncementVo);
        return BaseResult.success("新增管理成功");
    }

    @ApiOperation("(平台动态,信息公告,政策法规)-列表 条件查询")
    @GetMapping("/list")
    public BaseResult list(ProcurementList procurementList)
    {
        startPage();
        List<ProcurementList> list = procurementListService.selectProcurementListList(procurementList);
        return BaseResult.success(list);
    }

    /**
     * 导出采购-公告法规动态列表
     */
    @ApiOperation("到处采购信息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcurementList procurementList)
    {
        procurementList.setListType(1);
        List<ProcurementList> list = procurementListService.selectProcurementListList(procurementList);
        ExcelUtil<ProcurementList> util = new ExcelUtil<ProcurementList>(ProcurementList.class);
        util.exportExcel(response, list, "采购-公告法规动态数据");
    }

    /**
     * 修改采购-公告法规动态
     */
    @ApiOperation("修改采购")
    @PutMapping("/updateProcurementList")
    public BaseResult edit(@RequestBody ProcurementList procurementList)
    {
        return BaseResult.success(procurementListService.updateProcurementList(procurementList));
    }

    /**
     * 删除采购-公告法规动态
     */
    @ApiOperation("删除采购")
    @DeleteMapping("/{listIds}")
    public BaseResult remove(@PathVariable Long[] listIds)
    {
        return BaseResult.success(procurementListService.deleteProcurementListByListIds(listIds));
    }
}
